#include <iostream>
#include "common.hpp"
#include "InetAddr.hpp"
#include<cstring>
void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " server_ip server_port" << std::endl;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);
    //创建套接字
    int sockfd= socket(AF_INET,SOCK_STREAM,0);
     if(sockfd < 0)
    {
        std::cerr << "socket error" << std::endl;
        exit(SOCKET_ERR);
    }
    //创建连接，底层自动绑定，不需要显示绑定
    InetAddr peer(serverip,serverport);
    int n=connect(sockfd,peer.NetAddrPtr(),peer.NetAddrLen());
    if(n<0)
    {
        std::cerr << "connect error" << std::endl;
        exit(CONNECT_ERR);
    }
    while(true)
    {
        std::cout<<"Please Enter@ ";
        std::string line;
        std::getline(std::cin,line);
        write(sockfd,line.c_str(),line.size());
        char buffer[1024];
        int n=read(sockfd,buffer,sizeof(buffer)-1);
         if(n > 0)
        {
            buffer[n] = 0;
            std::cout << "server echo# " << buffer << std::endl;
        }
    }
    close(sockfd);
    return 0;
}